package elijah.base.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import elijah.base.R;
import elijah.base.binding.TextBindable;
import elijah.base.databinding.ViewBarBinding;

public class BarView extends BaseWidget<ViewBarBinding> implements TextBindable {


    public BarView(@NonNull Context context) {
        super(context);
    }

    public BarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initView(@Nullable AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.BarView);
            binding.header.setText(typedArray.getString(R.styleable.BarView_headerText));
            binding.header.setTextColor(typedArray.getColor(R.styleable.BarView_headerTextColor, binding.header.getCurrentTextColor()));
            binding.header.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimension(R.styleable.BarView_headerTextSize, binding.header.getTextSize()));
            binding.content.setText(typedArray.getString(R.styleable.BarView_contentText));
            binding.content.setHint(typedArray.getString(R.styleable.BarView_android_hint));
            binding.content.setTextColor(typedArray.getColor(R.styleable.BarView_contentTextColor, binding.content.getCurrentTextColor()));
            binding.content.setTextSize(TypedValue.COMPLEX_UNIT_PX, typedArray.getDimension(R.styleable.BarView_contentTextSize, binding.content.getTextSize()));
            binding.content.setBackgroundColor(typedArray.getColor(R.styleable.BarView_contentBgColor, Color.WHITE));
            Drawable icon = typedArray.getDrawable(R.styleable.BarView_android_drawableEnd);
            int pad = typedArray.getDimensionPixelSize(R.styleable.BarView_android_drawablePadding, 0);
            if (icon != null) {
                binding.content.setCompoundDrawables(null, null, null, icon);
                binding.content.setCompoundDrawablePadding(pad);
            }
            typedArray.recycle();
        }



    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        ViewGroup.LayoutParams layoutParams = binding.item.getLayoutParams();
        layoutParams.height = getMeasuredHeight();
        binding.item.setLayoutParams(layoutParams);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public void setContentText(String text) {

        binding.content.setText(text);


    }


    public void setContentTextColor(int color) {
        binding.content.setTextColor(color);
    }

    public void setContentBackgroundColor(int color) {
        binding.content.setBackgroundColor(color);
    }


    @Override
    public void setText(String text) {
        setContentText(text);

    }

    @Override
    public String getText() {
        return binding.content.getText().toString();
    }

    public TextView getContentView() {
        return binding.content;
    }
}
